/**
  ******************************************************************************
  * @file    Key.c
  * @author  Qilong Huang
  * @version V0.0.1
  * @date    04-03-2020
  * @brief   This file contains functions to initialize the OS.  
  *				 
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/

/*===============================================[ private includes  ]================================================*/

#include "stm32f10x.h"
#include "Key.h"
#include "switch.h"
#include "can_canopen.h"
#include "Icons.h"
/*===============================================[ private defines   ]================================================*/
#define DEBOUNCE_KEY_CONST	        20
#define LONG_KEY_CONST		  	    2000 
#define UP_DOWN_CONST		        1500
#define DEBOUNCE_KEY_PRESSED	    200
#define SHORT_KEY_CONST		  	    50 
#define KEY_COUNT_LOCK				0x01
#define KEY_COUNT_UNLOCK			0x00
/*===============================================[ private datatypes ]================================================*/

/*===============================================[ private variables ]================================================*/
//static u8 ShortEnterEable = 0;


/*===============================================[ public variables ]================================================*/
u8 key_serviced = 0;//just for enter, left ,right key
volatile u16 key_up_value;
volatile u16 key_up_time_value;
volatile u16 key_down_value;
volatile u16 key_down_time_value;
volatile u16 key_enter_value;


U8 Key_status[4];
U8 SwitchKeyStatus;
static U8 start_flag = 0;
U16 SoftCloseCounter;
U8 SoftCloseFlag = FALSE;

/*===============================================[ private functions ]================================================*/

/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/


/*================================================[ public definions ]================================================*/



/**
* @brief	This task samples the pushbuttons
* @param	none
* @note     none
* @retval   none
*/
void sample_PB_task(void const *argument) 
{
    static u16 key1_count = 0, key2_count = 0, key3_count = 0, key4_count = 0;
    static u16 key1_lock = 0, key2_lock = 0, key3_lock = 0, key4_lock = 0;
    Pin_Code.PinCode_Pt = 0;
    while (TRUE)
    {
        if((switches_current & KEY4_IN) == 0) 
        {
            if(key4_lock == KEY_COUNT_UNLOCK)		//key4 is active
            {
                key4_count ++;
                if(key4_count >= SHORT_KEY_CONST)
                {
                    Key_status[0] = KEY_SHORT_PRESS;
                    key4_lock = KEY_COUNT_LOCK;		//key4 is locked
                }				
            }
        }
        if((switches_current & KEY3_IN) == 0) 
        {
            if(key3_lock == KEY_COUNT_UNLOCK)		//key3 is active
            {
                key3_count ++;
                if(key3_count >= SHORT_KEY_CONST)
                {
                    Key_status[1] = KEY_SHORT_PRESS;
                    key3_lock = KEY_COUNT_LOCK;		//key3 is locked
                }				
            }
        }
        if((switches_current & KEY2_IN) == 0) 
        {
            if(key2_lock == KEY_COUNT_UNLOCK)		//key2 is active
            {
                key2_count ++;
                if(key2_count >= SHORT_KEY_CONST)
                {
                    Key_status[2] = KEY_SHORT_PRESS;
                    key2_lock = KEY_COUNT_LOCK;		//key2 is locked
                }				
            }
            if(key2_lock == KEY_COUNT_LOCK)         //key2 long key input detect
            {
                key2_count ++;
                if(key2_count >= LONG_KEY_CONST)
                {
                    Key_status[2] = KEY_LONG_PRESS;
                }
            }
        }
        if((switches_current & KEY1_IN) == 0) 
        {
            if(key1_lock == KEY_COUNT_UNLOCK)		//key1 is active
            {
                key1_count ++;
                if(key1_count >= SHORT_KEY_CONST)
                {
                    Key_status[3] = KEY_SHORT_PRESS;
                    key1_lock = KEY_COUNT_LOCK;		//key1 is locked
                }				
            }
        }
        if((switches_current & KEY_SWITCH_IN) == 0)                      //Turn off the dshb
        {
            Screen_Mode = MODE_Standby;
            start_flag = 0;
        }
        else if((switches_current & KEY_SWITCH_IN) == KEY_SWITCH_IN)    //Turn on the Dshb
        {
            if(start_flag == 0)
            {
                start_flag = 1;
                Screen_Mode = MODE_PinCode;
            }
        }
        if((switches_current & 0x1f) == 0x1f)		//release all the key
        {
            key1_lock = KEY_COUNT_UNLOCK;
            key1_count = 0;
            key2_lock = KEY_COUNT_UNLOCK;
            key2_count = 0;
            key3_lock = KEY_COUNT_UNLOCK;
            key3_count = 0;
            key4_lock = KEY_COUNT_UNLOCK;
            key4_count = 0;
        }
        key_handler();

        task_status[SAMPLE_PB_ID] = ALIVE;
        osDelay(SAMPLE_PB_ITV);
    }  // end while infinite loop
}  // end sample_PB_task()

/**
* @brief	This task samples the pushbuttons
* @param	none
* @note     none
* @retval   none
*/
void key_handler(void)
{
    //Pin code input mode
    static Screen_Mode_type prev_mode;
    u8 i = 0;
    if((Test_step_counter == 1) || (Test_step_counter == 2) || (Test_step_counter == 3) || (Test_step_counter == 4) || (Test_step_counter == 5) \
    || (Test_step_counter == 6) || (Test_step_counter == 7)  || (Test_step_counter == 9) || (Test_step_counter == 10) || (Test_step_counter == 14) \
    || (Test_step_counter == 15) || (Test_step_counter == 16) || (Test_step_counter == 18) || (Test_step_counter == 19) || (Test_step_counter == 20) \
    || (Test_step_counter == 21) || (Test_step_counter == 22) || (Test_step_counter == 23) || (Test_step_counter == 24) || (Test_step_counter == 25)\
    || (Test_step_counter == 26) || (Test_step_counter == 27)  || (Test_step_counter == 28))
    {
        if(Key_status[0] == KEY_SHORT_PRESS)
        {
            Test_step_counter ++;
            Key_status[0] = RESET_KEY_STATUS;
        }
        if(Key_status[1] == KEY_SHORT_PRESS)
        {
            Key_status[1] = RESET_KEY_STATUS;
        }
        if(Key_status[2] == KEY_SHORT_PRESS)
        {
            Key_status[2] = RESET_KEY_STATUS;
        }
        if(Key_status[3] == KEY_SHORT_PRESS)
        {
            Key_status[3] = RESET_KEY_STATUS;
        }		
    }
    else if(Test_step_counter == 11)
    {
        if(Key_status[0] == KEY_SHORT_PRESS)
        {
            Key_status[0] = RESET_KEY_STATUS;
        }
        if(Key_status[1] == KEY_SHORT_PRESS)
        {
            Test_step_counter ++;
            Key_status[1] = RESET_KEY_STATUS;
        }
        if(Key_status[2] == KEY_SHORT_PRESS)
        {
            Key_status[2] = RESET_KEY_STATUS;
        }
        if(Key_status[3] == KEY_SHORT_PRESS)
        {
            Key_status[3] = RESET_KEY_STATUS;
        }		        
    }
    else if(Test_step_counter == 12)
    {
        if(Key_status[0] == KEY_SHORT_PRESS)
        {
            Key_status[0] = RESET_KEY_STATUS;
        }
        if(Key_status[1] == KEY_SHORT_PRESS)
        {
            Key_status[1] = RESET_KEY_STATUS;
        }
        if(Key_status[2] == KEY_SHORT_PRESS)
        {
            Test_step_counter ++;
            Key_status[2] = RESET_KEY_STATUS;
        }
        if(Key_status[3] == KEY_SHORT_PRESS)
        {
            Key_status[3] = RESET_KEY_STATUS;
        }		        
    }
    else if(Test_step_counter == 13)
    {
        if(Key_status[0] == KEY_SHORT_PRESS)
        {
            Key_status[0] = RESET_KEY_STATUS;
        }
        if(Key_status[1] == KEY_SHORT_PRESS)
        {
            Key_status[1] = RESET_KEY_STATUS;
        }
        if(Key_status[2] == KEY_SHORT_PRESS)
        {
            Key_status[2] = RESET_KEY_STATUS;
        }
        if(Key_status[3] == KEY_SHORT_PRESS)
        {
            Test_step_counter ++;
            Key_status[3] = RESET_KEY_STATUS;
        }		        
    }
    else
    {
        if(Key_status[0] == KEY_SHORT_PRESS)
        {
            Key_status[0] = RESET_KEY_STATUS;
        }
        if(Key_status[1] == KEY_SHORT_PRESS)
        {
            Key_status[1] = RESET_KEY_STATUS;
        }
        if(Key_status[2] == KEY_SHORT_PRESS)
        {
            Key_status[2] = RESET_KEY_STATUS;
        }
        if(Key_status[3] == KEY_SHORT_PRESS)
        {
            Key_status[3] = RESET_KEY_STATUS;
        }		        
    }
}// end key_handler()

void set_digicode(void)
{
    u32 pin_code_preset_number;
    u8 pin_code_high = 0;
    u8 pin_code_sheft = 0;
    pin_code_high = Pin_Code.PinCodeNum[0] * 10 + Pin_Code.PinCodeNum[1];
    if((Pin_Code.PinCodeNum[1] == 1) || (Pin_Code.PinCodeNum[1] == 2))
    {
        pin_code_sheft = (Pin_Code.PinCodeNum[1] - 1) * 16 + (Pin_Code.PinCodeNum[2] - 1) * 4 + Pin_Code.PinCodeNum[3] - 1;
    }
    else if((Pin_Code.PinCodeNum[1] == 3) || (Pin_Code.PinCodeNum[1] == 4))
    {
        pin_code_sheft = (Pin_Code.PinCodeNum[1] - 3) * 16 + (Pin_Code.PinCodeNum[2] - 1) * 4 + Pin_Code.PinCodeNum[3] - 1;
    }
    pin_code_preset_number = 0x80000000 >> pin_code_sheft;
    switch(pin_code_high)
    {
        case 11:
        case 12:
        {
            DigicodeArray1Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY1;
            menu_needs_ee = 1;
            break;
        }
        case 13:
        case 14:
        {
            DigicodeArray2Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY2;
            menu_needs_ee = 1;
            break;
        }
        case 21:
        case 22:
        {
            DigicodeArray3Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY3;
            menu_needs_ee = 1;
            break;
        }
        case 23:
        case 24:
        {
            DigicodeArray4Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY4;
            menu_needs_ee = 1;
            break;
        }
        case 31:
        case 32:
        {
            DigicodeArray5Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY5;
            menu_needs_ee = 1;
            break;
        }
        case 33:
        case 34:
        {
            DigicodeArray6Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY6;
            menu_needs_ee = 1;
            break;
        }
        case 41:
        case 42:
        {
            DigicodeArray7Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY7;
            menu_needs_ee = 1;
            break;
        }
        case 43:
        case 44:
        {
            DigicodeArray8Value |= pin_code_preset_number; 
            menu_ee_index = VAR_USER_DIGICODEARRAY8;
            menu_needs_ee = 1;
            break;
        }
        default:break;
    }        
}

/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/

